#include "stdafx.h"
#include <sgp/VideoManager.h>
#include "InitGameHandler.h"
#include "IntroGameScreen.h"

namespace ja2
{
	InitGameHandler::InitGameHandler(sgp::VideoManager &VideoManager, IGameStateDirector &GameStateDirector)
		: m_VideoManager(VideoManager),
		m_SubScreen(255),
		m_GameStateDirector(GameStateDirector)
	{
	}

	void InitGameHandler::process()
	{
		if(m_SubScreen == 255)
		{
			// Splash is shown
			if(m_InitState.done_with_splash_screen())
				m_SubScreen = 0;
			// Need to run intro
			else
			{
				// Hide cursor
				m_VideoManager.hide_cursor();
				// Next screen will be intro
				m_pScreen = GameScreenPtr_t(new IntroGameScreen(m_InitState));
			}

		}
#if 0
		if ( ubCurrentScreen == 0 )
		{
			if ( strcmp( gzCommandLine, "-NODD" ) == 0 )
			{
				gfDontUseDDBlits = TRUE;
			}

			// Load version number....
			//HandleLimitedNumExecutions( );

			ubCurrentScreen = 1;

			// Init screen

			// Set Font
			SetFont( TINYFONT1 );
			SetFontBackground( FONT_MCOLOR_BLACK );
			SetFontForeground( FONT_MCOLOR_WHITE );

			//mprintf( 10, 420, zVersionLabel );

#ifdef _DEBUG
			mprintf( 10, 10, L"%s: %s Debug %S", pMessageStrings[ MSG_VERSION ], zVersionLabel, czVersionNumber );
#else
			mprintf( 10, 10, L"%s: %s %S", pMessageStrings[ MSG_VERSION ], zVersionLabel, czVersionNumber );
#endif

#if defined JA2BETAVERSION

			mprintf( 10, 0, L"(Beta version error reporting enabled)" );

#endif

#ifdef _DEBUG
			mprintf( 10, 20, L"SOLDIERTYPE: %d bytes", sizeof( SOLDIERTYPE ) );
#endif

			if ( gfDontUseDDBlits )
			{
#ifdef _DEBUG
				mprintf( 10, 10, L"SOLDIERTYPE: %d bytes", sizeof( SOLDIERTYPE ) );
#else
				mprintf( 10, 20, L"Using software blitters" );
#endif
			}

			// Remove this for real release
			// mprintf( 10, 40, L"(Space Viking's Many Mercs beta 007n -- 29 December 2008)" );

			// Handle queued .ini file error messages
			int y = 40;
#ifdef USE_VFS
			CLog logger(L"ERROR_REPORT.iniErrorMessages.txt");
			//logger.SetAppend();
#endif
			while (! iniErrorMessages.empty()) {
#ifndef USE_VFS
				FILE *file_pointer;
#endif
				static BOOL iniErrorMessage_create_out_file = TRUE;
				std::string iniErrorMessage = iniErrorMessages.top();
				CHAR16 str[256];

				if (iniErrorMessage_create_out_file)
				{
#ifndef USE_VFS
					fopen_s( &file_pointer, "..\\ERROR_REPORT.iniErrorMessages.txt", "w" );
#endif
					y += 25;
					swprintf( str, L"%S", "ERROR_REPORT.iniErrorMessages.txt has been created. Please review its content." );
					DisplayWrappedString( 10, y, 560, 2, FONT12ARIAL, FONT_RED, str, FONT_BLACK, TRUE, LEFT_JUSTIFIED );
					iniErrorMessage_create_out_file = FALSE;
				}
				else
				{
#ifndef USE_VFS
					fopen_s( &file_pointer, "..\\ERROR_REPORT.iniErrorMessages.txt", "a+" );
#endif
				}

				y += 25;
				swprintf( str, L"%S", iniErrorMessage.c_str() );
#ifndef USE_VFS
				fprintf_s (file_pointer , "%S\n"  , str );
				fclose( file_pointer );
#else
				logger << iniErrorMessage << CLog::ENDL;
#endif
				DisplayWrappedString( 10, y, 560, 2, FONT12ARIAL, FONT_RED, str, FONT_BLACK, TRUE, LEFT_JUSTIFIED );
				iniErrorMessages.pop();

				if (iniErrorMessages.empty()) {for(int x=0 ; x <= 65535*2 ; x++);}
			}

			InvalidateScreen( );

			SetCurrentCursorFromDatabase( VIDEO_NO_CURSOR );

			return( INIT_SCREEN );
		}

		if ( ubCurrentScreen == 1 )
		{
			ubCurrentScreen = 2;
			return( InitializeJA2( ) );
		}

		if ( ubCurrentScreen == 2 )
		{
			InitMainMenu( );
			ubCurrentScreen = 3;
			return( INIT_SCREEN );
		}

		// Let one frame pass....
		if ( ubCurrentScreen == 3 )
		{
			ubCurrentScreen = 4;
			SetCurrentCursorFromDatabase( VIDEO_NO_CURSOR );
			return( INIT_SCREEN );
		}

		if ( ubCurrentScreen == 4 )
		{
			SetCurrentCursorFromDatabase( VIDEO_NO_CURSOR );
			InitNewGame( FALSE );
		}
		return( INIT_SCREEN );
*/
#endif
		// Show the screen
		m_pScreen->handle();
	}
} /*ja2*/